import {_decorator, Component, director, ProgressBar,loader} from 'cc';
import Platform, {PlatformType} from "./Platform";
import LevelCfgItem from "./LevelCfgItem";
import {GameMain} from "./GameMain";
//import Platform from "./Platform";
const { ccclass, property } = _decorator;

//游戏Loading加载页面逻辑
 
@ccclass('Loading')
export class Loading extends Component {
    @property({type:ProgressBar})
    progressBar:ProgressBar=null;

    onLoad()
    {
        this.progressBar.progress=0;

        Platform.decidePlatform();
    }

    start()
    {
        this.startLoading();
    }

    private startLoading()
    {
        let startTime:number=Date.now();

        let path="levels/levels";
        loader.loadRes(path, (err, res) => {
            if (err) {
                console.log("load res error=" + err);
            } else {
                //console.log("levels data="+res.text);
                let cfgData = JSON.parse(res.text);

                //console.log("** JSON="+ JSON.stringify(cfgData));
                for(let i=0;i<cfgData.levels.length;i++)
                {
                    let levelCfgItem:LevelCfgItem=new LevelCfgItem();
                    levelCfgItem.levelId=cfgData.levels[i].id;
                    levelCfgItem.levelType=cfgData.levels[i].type;
                    levelCfgItem.diff=cfgData.levels[i].diff;
                    if(levelCfgItem.levelType===0)
                    {
                        levelCfgItem.parse(cfgData.levels[i].condition);
                        GameMain.levelCfg.push(levelCfgItem);
                    }
                    else if(levelCfgItem.levelType===1)
                    {
                        levelCfgItem.stepLimit=cfgData.levels[i].step;
                        levelCfgItem.parseLineGame(cfgData.levels[i].balls);
                        GameMain.levelCfg.push(levelCfgItem);
                    }
                }
                //console.log("** Cfg="+JSON.stringify(this.levelCfg));

                this.loadGameScene();
            }
        });
    }

    private loadGameScene()
    {
        director.preloadScene("game", (completedCount, totalCount, item) =>
            {
                let progress:number=completedCount / totalCount;
                if(progress>1)
                    progress=1;
                this.progressBar.progress=progress;
                //console.log("** 完成数="+completedCount+"  总数="+totalCount);
            },
            (error, asset) =>
            {
                //director.loadScene("game");
                if(Platform.currentPlatform===PlatformType.OPPO || Platform.isAndroid())
                {
                    this.scheduleOnce(()=>{
                        director.loadScene("game");
                    }, 1);
                }
                else
                {
                    director.loadScene("game");
                    /*
                    this.scheduleOnce(()=>{

                    }, 0.5);*/
                }
            });
    }
}






















